package com.huika.renrench.control.project.activity;

import java.util.HashMap;

import android.opengl.Visibility;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.utils.DebugTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.login.activity.LoginActivity;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.model.ProjectDetail;
import com.huika.renrench.model.ProjectDetailX;
import com.huika.renrench.support.bean.EnshipProject;
import com.huika.renrench.support.bean.LocationBean;
import com.huika.renrench.utils.LoginUtils;
import com.huika.renrench.view.TitleBarHelper;

/**
 * @author cailu
 * @date：2015年10月22,下午1:14
 * @describe:创业项目详情activity
 */
public class ProjectDetailActivity extends BaseAct implements
		Listener<RequestResult<ProjectDetailX>> {
	public static final String PROJECT_TAG = "project";

	/** 顶部图片 */
	private ImageView iv_head_pic;
	/** 项目名称 */
	private TextView tv_name;

	/** 项目地址 */
	private TextView tv_location;
	/** 项目 描述 */
	private TextView tv_desc;

	/** 目标金额 */
	private TextView tv_targetPrice;
	/** 已筹金额 */
	private TextView tv_getPrice;

	// /** 距离 */
	// private TextView tv_owner_distance;
	private EnshipProject project;
	private ProjectDetailX projectDetail = new ProjectDetailX();
	/** title 设置对象 */
	private TitleBarHelper titleBarHelper;
	//
	/** item的id */
	private String projectId;
	private LocationBean locationBean;

	private TextView tv_getProgress;

	private ProgressBar pb_price_progressbar;

	private TextView tv_category;

	private ImageView iv_investImg;

	private LinearLayout ll_rongzi_flag;

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		super.initData();
		
		projectId= getIntent().getLongExtra("pid", 0L)+"";
				
		/** 获取网络数据 **/
		getNetData();

	}

	private void getNetData() {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("pid", projectId);
		// if (GlobalApp.getInstance().isLocated()) {
		// ajaxParams.putCommonTypeParam("latitude", locationBean.latitude);
		// ajaxParams.putCommonTypeParam("longitude", locationBean.longtitude);
		// }

		getUserId(ajaxParams);
		FormResultRequest<ProjectDetailX> getResultRequest = new FormResultRequest<ProjectDetailX>(
				UrlConstants.URL_PROJECT_DETAIL2, this, this,
				new TypeToken<RequestResult<ProjectDetailX>>() {
				}.getType());
		getResultRequest.setRequestParams(ajaxParams);
		executeRequest(getResultRequest);

	}

	@Override
	public void setRootView() {
		// 注入布局
		setContentView(R.layout.activity_chuangye_detail);
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		// 初始化标题
		initTitle();
		// 初始化上面部分
		initTopView();
		// 初始化下面部分 会做判断隐藏或者显示
		initBottomView();

		// getNetData();

	}

	private void initBottomView() {
		tv_targetPrice = (TextView) findViewById(R.id.cyda_target_price);
		tv_getPrice = (TextView) findViewById(R.id.cyda_get_price);
		tv_getProgress = (TextView) findViewById(R.id.cyda_tv_getProgress);
		pb_price_progressbar = (ProgressBar) findViewById(R.id.cyda_progressbar);
		ll_rongzi_flag = (LinearLayout) findViewById(R.id.cyda_ll_rongzi_flag);
	}

	private void initTitle() {

		// 猜测是用来显示标题的，这个我等以后来看看。
		titleBarHelper = new TitleBarHelper(this, -1, R.string.project_detail);

		titleBarHelper.setLeftMsg("返回");
		// TODO这里到时候要通过判断来选中图标

		titleBarHelper.setOnRightImgClickListener(this);
		titleBarHelper.setOnRightClickListener(this);

	}

	private void initTopView() {
		iv_head_pic = (ImageView) findViewById(R.id.cyda_detail_item_iv);
		tv_name = (TextView) findViewById(R.id.cyda_detail_item_headline);
		tv_category = (TextView) findViewById(R.id.cyda_detail_item_headline_two);

		tv_location = (TextView) findViewById(R.id.cyda_detal_item_headline_three);

		tv_desc = (TextView) findViewById(R.id.cyda_content_detail_text);
		iv_investImg = (ImageView) findViewById(R.id.cyda_iv_investImg);

		// tv_owner_name.setOnClickListener(this);
		// tv_owner_address.setOnClickListener(this);
		// findViewById(R.id.icon_tell).setOnClickListener(this);

	}

	@Override
	public void onStop() {
		super.onStop();
	}

	private void getUserId(JSONWrapAjaxParams ajaxParams) {
		String u_id = GlobalApp.getInstance().getUserId();
		if (!TextUtils.isEmpty(u_id)) {
			ajaxParams.putCommonTypeParam("userId", u_id);
		} else {
			ajaxParams.putCommonTypeParam("userId", "0");
		}
	}

	/** 复写该方法用来写页面中点击事件 **/
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {

		case R.id.title_right_iv:
			if (!LoginUtils.isLogin()) {
				showActivity(this, LoginActivity.class);
				return;
			}
			// 添加进收藏夹TODO
			DebugTool.print("没有接口");
			getAddCollect();

			break;
		}

	}

	private void getAddCollect() {
		showLoadingDialog(getString(R.string.common_loading));
		// http://192.168.16.219:8082/rrcpro-api/loanInvestService/AttionPro
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		String u_id = GlobalApp.getInstance().getUserId();
		if (TextUtils.isEmpty(u_id)) {
			return;
		}
		ajaxParams.putCommonTypeParam("pid", projectId);
		ajaxParams.putCommonTypeParam("userId", u_id);
		getUserId(ajaxParams);
		FormResultRequest<HashMap<String, String>> getResultRequest = new FormResultRequest<HashMap<String, String>>(
				UrlConstants.ADDCOLLECT2, getAddCollectRsp, this,
				new TypeToken<RequestResult<HashMap<String, String>>>() {
				}.getType());
		getResultRequest.setRequestParams(ajaxParams);
		executeRequest(getResultRequest);

	}

	private Listener<RequestResult<HashMap<String, String>>> getAddCollectRsp = new Listener<RequestResult<HashMap<String, String>>>() {

		@Override
		public void onResponse(RequestResult<HashMap<String, String>> result) {
			if (result != null && result.flag == 1) {
				showToastMsg("关注成功！");
				titleBarHelper.setRightImg(R.drawable.title_attention_icon);
				titleBarHelper.setOnRightClickListener(null);
			} else {
			}

			dismissLoadingDialog();
		}
	};

	@Override
	public void onResponse(RequestResult<ProjectDetailX> result) {
		dismissLoadingDialog();
		if (result.flag == 1) {
			if (result.getRs() != null) {
				projectDetail = result.getRs();

				if (null != projectDetail.logoPath) {
					GlobalApp
							.loadImgLoader(iv_head_pic, projectDetail.logoPath);
				}
				tv_name.setText(projectDetail.name);
				tv_category.setText(projectDetail.categoryIds);
				tv_location.setText(projectDetail.location);
				tv_desc.setText(projectDetail.description);
				if (null != projectDetail.logoPath) {
					GlobalApp.loadImgLoader(iv_investImg,
							projectDetail.investImg);
				}
				//

				tv_targetPrice.setText(projectDetail.amount);
				tv_getPrice.setText(projectDetail.amountHad);
				tv_getProgress
						.setText("进度" + projectDetail.amountPercent + "%");
				pb_price_progressbar.setProgress((int) Double
						.parseDouble((projectDetail.amountPercent)));

				if ("1".equals(projectDetail.isAttention)) {
					titleBarHelper.setRightImg(R.drawable.title_attention_icon);
					titleBarHelper.setOnRightClickListener(null);
					// titleBarHelper.setRightMsgColor(getResources().getColor(
					// R.color.color_969696));

				} else {
					titleBarHelper
							.setRightImg(R.drawable.title_noattention_icon);
					titleBarHelper.setOnRightClickListener(this);
				}
				
				if ("6".equals(projectDetail.state)) {
					
					ll_rongzi_flag.setVisibility(View.VISIBLE);
				}

			}

		}

	}

	@Override
	public void onErrorResponse(VolleyError error) {
		super.onErrorResponse(error);
	}

}
